<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inv_release extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('variable');
	}

	public function index(){

	  $this->load->view('layouts/header',$this->variable->load_data($this->session->userdata('title')));
	  $this->load->view($this->session->userdata('formain'));
	  $this->load->view('layouts/footer');
	}
	public function add_form(){
				$this->load->view($this->session->userdata('foradd'),array(
						'reqs'	=>	$this->inv_release_model->get_request()
					));
	}
	public function edit_form($id){

			$this->load->view($this->session->userdata('foredit'),array(
				));
		
	}
	public function view_form($id){
			$this->load->view($this->session->userdata('forview'),array(
				));
	}


	public function get_req_content(){
		$result = $this->inv_release_model->get_request_id($this->input->post('req_id'));
		// $seq = 10;
		// $result2 = $this->inv_release_model->get_location_items($result[0]->inv_request_id);
		// foreach ($result2 as $key) {
		// 	if($key->left )
		// }
		echo json_encode(array('result' =>$result));
	}

	public function get_content(){

		$this->inv_release_model->get_contents($this->input->post('reqs_id'));
	}

	public function get_picking(){
		$result =  $this->inv_release_model->get_picks($this->input->post('reqcon_id'));
		echo json_encode($result);
	}



	public function confirm_picking(){
		$req = $this->input->post('reqQty');
		$rel = $this->input->post('relQty');
		$result =  $this->inv_release_model->get_picks($this->input->post('reqcon_id'));
		$needed = $req - $rel;
		$allotted = $needed;
		$seq = 10;
		foreach ($result as $key) {
			if( $key->item_qty - $needed >= 0 && $needed != 0)   {
                      $left = $key->item_qty - $needed;
                      // trHTML += '<tr style="line-height:0;padding:2px;"><td style="text-align:left; padding:5px;">'+seq+'</td><td style="text-align:left; padding:5px;">'+data[i].loc_desc+'</td><td id="request_qty" style="text-align:left; padding:5px;">'+data[i].i_desc+'</td><td style="text-align:left; padding-left:5px;">'+data[i].item_qty+'</td><td id="" style="text-align:left; padding-left:5px;">'+needed+'</td><td id="" style="text-align:left; padding-left:5px;">'+left+'</td></tr>';
                      
                      $data = array(
                      	':p_release_id'		=>	$this->input->post('rel_id'),
                      	':p_request_id'		=>	$this->input->post('req_id'),
                      	':p_req_content_id'	=>	$this->input->post('reqcon_id'),
                      	':p_seq_num'		=>	$seq,
                      	':p_item_id'		=>	$key->item_id,
                      	':p_inv_sub_id'		=>	$key->inv_sub_id,
                      	':p_locator_id'		=>	$key->locator_id,
                      	':p_release_qty'	=>	$needed,
                      	':p_user_id'		=>	$this->session->userdata('user_id')
                      );
                      $res = $this->inv_release_model->insert_contents_req($data);
                      $seq = $seq + 10;
                      $needed = $needed-$needed;    

        	}else if($needed != 0){
                      $left = 0;
                      
                      $data = array(
                      	':p_release_id'		=>	$this->input->post('rel_id'),
                      	':p_request_id'		=>	$this->input->post('req_id'),
                      	':p_req_content_id'	=>	$this->input->post('reqcon_id'),
                      	':p_seq_num'		=>	$seq,
                      	':p_item_id'		=>	$key->item_id,
                      	':p_inv_sub_id'		=>	$key->inv_sub_id,
                      	':p_locator_id'		=>	$key->locator_id,
                      	':p_release_qty'	=>	$key->item_qty,
                      	':p_user_id'		=>	$this->session->userdata('user_id')
                      );
                      $res = $this->inv_release_model->insert_contents_req($data);
                      $seq = $seq + 10;
                      $needed = $needed - $key->item_qty;

         	} 
		}
		echo json_encode($result);
	}

	public function insert_release(){
		$cur_req = $this->inv_release_model->get_current_status($this->input->post('request_id'));
		$data = array(
			':p_request_id'		=>	$this->input->post('request_id'),
			':p_request_date'	=>	date('Y-m-d'),
			':p_request_status'	=> 	($cur_req[0]->status == 'REQ_APPROVED' ? '' : $cur_req[0]->status ),
			':p_user_id'		=>	$this->session->userdata('user_id')
			);
		$this->inv_release_model->insert_release($data);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */